/*****************
验证是否可以用channel来实现限流的操作
******************/
package main

import (
	"fmt"
	"sync"
	"time"
)

const size = 10

func main() {
	ch := make(chan int, size)
	var wg sync.WaitGroup
	for x := 0; x < 20; x++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			l := len(ch)
			if l >= size {
				fmt.Printf("len size=%d is greater or Equal  10 \n", l)
				return
			}

			ch <- 1
			defer func() {
				<-ch //释放空间
			}()
			fmt.Println("len is %d", l)
			time.Sleep(time.Second) //浅睡一下 ....
		}()
		time.Sleep(50 * time.Millisecond)
	}
	wg.Wait()
}
